package tt.dz.order.pojo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;

/**
 * @Title 订单项分成明细
 * @author zhoutian   
 * @date 2015-11-26
 */
public class OrderItemDividedDetail {
	//订单编号(联合主键)
	private String orderNo;
	//订单订单项id(联合主键)
	private String itemId;
	//服务编号(联合主键)
	private String serviceNo;
	//提供服务的运营商编号
	private String serviceProvider;
	//该运营商提供的服务是否是成本类的服务，0：否 1：是
	private int isCost;
	//收入的运营商
	private String receiver;
	//当前的分账比例
	private BigDecimal currentRatio;
	//原始价格,活动后的价格
	private BigDecimal totalPrice;
	//分润后的价格
	private BigDecimal splitedPrice;
	//保底金,有收益时需要支付给该第三方运营商的最低金额
	private BigDecimal minGuarantee;
	//创建时间
	private Date createTime;

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getServiceProvider() {
		return serviceProvider;
	}

	public void setServiceProvider(String serviceProvider) {
		this.serviceProvider = serviceProvider;
	}

	public int getIsCost() {
		return isCost;
	}

	public void setIsCost(int isCost) {
		this.isCost = isCost;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public BigDecimal getCurrentRatio() {
		return currentRatio;
	}

	public void setCurrentRatio(BigDecimal currentRatio) {
		this.currentRatio = currentRatio;
	}

	public BigDecimal getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(BigDecimal totalPrice) {
		this.totalPrice = totalPrice;
	}

	public BigDecimal getSplitedPrice() {
		return splitedPrice;
	}

	public void setSplitedPrice(BigDecimal splitedPrice) {
		this.splitedPrice = splitedPrice;
	}

	public BigDecimal getMinGuarantee() {
		return minGuarantee;
	}

	public void setMinGuarantee(BigDecimal minGuarantee) {
		this.minGuarantee = minGuarantee;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public OrderItemDividedDetail() {
		
	}
	
	public OrderItemDividedDetail(OrderItem orderItem, String merchantCode, BigDecimal ratio, BigDecimal minAmount, BigDecimal splitedPrice) {
		this.orderNo = orderItem.getOrderNo();
		this.itemId = orderItem.getItemId();
		this.serviceNo = orderItem.getServiceNo();
		this.serviceProvider = orderItem.getServiceProvider();
		this.isCost = orderItem.getIsCost();
		this.receiver = merchantCode;
		this.currentRatio = ratio;
		this.totalPrice = orderItem.getRealTotalPrice();
		this.splitedPrice = splitedPrice;
		this.minGuarantee = minAmount;
		this.createTime = new Date();
	}
	
	public OrderItemDividedDetail(OrderItem orderItem) {
		this.orderNo = orderItem.getOrderNo();
		this.itemId = orderItem.getItemId();
		this.serviceNo = orderItem.getServiceNo();
		this.serviceProvider = orderItem.getServiceProvider();
		this.isCost = orderItem.getIsCost();
		this.receiver = orderItem.getServiceProvider();
		this.currentRatio = null;
		this.totalPrice = orderItem.getRealTotalPrice();
		this.splitedPrice = new BigDecimal(this.totalPrice.toString());
		this.minGuarantee = null;
		this.createTime = new Date();
	}
	
	public void save(){
		String sql = "insert into OrderItem_Divided_Detail(itemid, orderNo, serviceNo, serviceProvider, isCost, receiver, currentRatio, totalPrice, splitedPrice, minGuarantee, createTime) values(:itemId,:orderNo, :serviceNo, :serviceProvider, :isCost, :receiver, :currentRatio, :totalPrice, :splitedPrice, :minGuarantee, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	/**
	 * 根据订单号获取订单项
	 * @return 订单列表
	 */
	public static List<OrderItemDividedDetail> getByOrderNo(String orderNo){
		String sql = "select itemid,orderNo, serviceNo, serviceProvider, isCost, receiver, currentRatio, totalPrice, splitedPrice, minGuarantee, createTime from OrderItem_Divided_Detail where orderNo = :orderNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderNo", orderNo);
		return BaseDAOUtil.queryForList(sql, paramMap, OrderItemDividedDetail.class);
	}
	/**
	 * 根据itemID获取分润列表
	 * @return 分润列表
	 */
	public static List<OrderItemDividedDetail> getByOrderNoAndServiceNo(String itemId){
		String sql = "select itemId,orderNo, serviceNo, serviceProvider, isCost, receiver, currentRatio, totalPrice, splitedPrice, minGuarantee, createTime from OrderItem_Divided_Detail where itemId = :itemId  ";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("itemId", itemId);
		return BaseDAOUtil.queryForList(sql, paramMap, OrderItemDividedDetail.class);
	}
	/**
	 * 修改分润金额
	 */
	public void updateOrderItemDividedDetail(){
		String sql = "update OrderItem_Divided_Detail set splitedprice=:splitedPrice,totalprice=:totalPrice  where itemId=:itemId and orderno=:orderNo and serviceno=:serviceNo and receiver=:receiver";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("splitedPrice", splitedPrice);
		paramMap.put("totalPrice", totalPrice);
		paramMap.put("orderNo", orderNo);
		paramMap.put("serviceNo", serviceNo);
		paramMap.put("receiver", receiver);
		paramMap.put("itemId", itemId);
		BaseDAOUtil.update(sql, paramMap);
	}
}
